﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using NHibernate;
using SocialLearning;

/// <summary>
/// Summary description for Chat_Message
/// </summary>
public partial class Chat_Message_Control
{
    public AMP_Message getMessage(int? MessageID)
    {
        // Open the session
        ISession session = NHibernateHelper.GetCurrentSession();

        // Load the message from the database
        AMP_Message Message = (AMP_Message)session.Load(typeof(AMP_Message), MessageID);

        return Message;
    }

    public List<AMP_Message> getMessages()
    {
        List<AMP_Message> Messages = new List<AMP_Message>();

        // Open the session
        ISession session = NHibernateHelper.GetCurrentSession();

        // Load the Messages from the database
        session.CreateCriteria(typeof(AMP_Message)).List(Messages);

        return Messages;
    }

    public void saveOrUpdateMessage(AMP_Message Message)
    {
        ISession session = NHibernateHelper.GetCurrentSession();
        ITransaction transaction = null;

        try
        {
            // Start transaction
            transaction = session.BeginTransaction();

            // Save or update (saves if the id is null, otherwise it updates)
            session.SaveOrUpdate(Message);

            // Commit transaction
            transaction.Commit();
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            throw ex;
        }
    }

    public void deleteMessage(AMP_Message Message)
    {
        ISession session = NHibernateHelper.GetCurrentSession();
        ITransaction transaction = null;

        try
        {
            // Start transaction
            transaction = session.BeginTransaction();

            // Delete
            session.Delete(Message);

            // Commit transaction
            transaction.Commit();
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            throw ex;
        }
    }
}

